/**
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2008，所有权利保留。
 * 
 * 项目名： rumba
 * 文件名： Welcome.java
 * 模块说明：    
 * 修改历史：
 * Mar 19, 2008 - linhui - 创建。
 */
package com.hd123.rumba.gwt.welcome.client;

import java.util.Date;
import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtext.client.core.RegionPosition;
import com.gwtext.client.widgets.DatePicker;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Viewport;
import com.gwtext.client.widgets.event.DatePickerListenerAdapter;
import com.gwtext.client.widgets.layout.BorderLayout;
import com.gwtext.client.widgets.layout.BorderLayoutData;
import com.gwtext.client.widgets.layout.FitLayout;
import com.gwtext.client.widgets.layout.VerticalLayout;
import com.hd123.rumba.gwt.base.client.EntryPointBase;
import com.hd123.rumba.gwt.base.client.RBRemoteServiceAsync;
import com.hd123.rumba.gwt.base.client.exception.ClientBizException;
import com.hd123.rumba.gwt.cartridge.client.CartridgeService;
import com.hd123.rumba.gwt.cartridge.client.CartridgeViewPanel;

/**
 * 欢迎界面
 * 
 * @author Linhui
 * 
 */
public class Welcome extends EntryPointBase {
  private final static String BORDERSTYLE_WHITE = "border: white;background: white;";
  private static final int VALUE_PNLEAST_WIGHT = 187;
  private static final int VALUE_PNLCARTRIDGE_WIDTH = 280;

  /** 界面部件，位于最顶层。 */
  private Panel mainPanel;

  /** 界面部件，顶端区域，位于{@link #mainPanel}内。 */
  private Panel northPanel;
  /** 界面部件，顶端构架包显示区域，位于{@link #northPanel}内。 */
  private Panel pnlCartridge;
  /** 界面部件，顶端欢迎信息显示区域，位于{@link #northPanel}内。 */
  private Panel pnlWelcome;

  /** 界面部件，右边区域，位于{@link #mainPanel}内。 */
  private Panel eastPanel;

  /** 界面部件，中心区域，位于{@link #mainPanel}内。 */
  private Panel centerPanel;

  public void onModuleLoad() {
    super.onModuleLoad();

    Panel panel = new Panel();
    panel.setBodyBorder(false);
    panel.setBodyStyle(BORDERSTYLE_WHITE);
    panel.setLayout(new FitLayout());

    mainPanel = new Panel();
    mainPanel.setBodyBorder(false);
    mainPanel.setBodyStyle(BORDERSTYLE_WHITE);

    mainPanel.setLayout(new BorderLayout());

    draw_north();
    draw_east();
    draw_center();

    panel.add(mainPanel);
    new Viewport(panel);

    loadAllCartridges();
  }

  private void draw_north() {
    northPanel = new Panel();
    northPanel.setBodyBorder(false);
    northPanel.setBaseCls("top");
    mainPanel.add(northPanel, new BorderLayoutData(RegionPosition.NORTH));

    draw_topWelcome();
    draw_topCartridge();
  }

  private void draw_topWelcome() {
    pnlWelcome = new Panel();
    pnlWelcome.setHeight("100%");
    pnlWelcome.setBaseCls("welcome-message");
    refreshWelcome();
    northPanel.add(pnlWelcome);
  }

  private void refreshWelcome() {
    try {
      pnlWelcome.setHtml("<b>" + this.getCurrentUser().getNameCode()
          + "</b> <span style='font-size: 12px;'>，欢迎您！</span>");
    } catch (ClientBizException e) {
      pnlWelcome.setHtml("");
    }
  }

  private void draw_topCartridge() {
    pnlCartridge = new Panel() {
      {
        setHeight("100%");
        setBaseCls("cartridge-panel");
      }
    };
    northPanel.add(pnlCartridge);
  }

  private void draw_east() {
    eastPanel = new Panel();
    eastPanel.setWidth(VALUE_PNLEAST_WIGHT);
    eastPanel.setLayout(new VerticalLayout(15));
    eastPanel.setBaseCls("east");
    mainPanel.add(eastPanel, new BorderLayoutData(RegionPosition.EAST));

    draw_Today();
    draw_Calendar();
  }

  private void draw_Today() {
    eastPanel.add(new Panel() {
      {
        setLayout(new VerticalLayout());
        setTitle("今天");
        setFrame(true);
        setWidth(VALUE_PNLEAST_WIGHT);
        add(new VerticalPanel() {
          {
            setWidth("100%");
            add(new HTML(Util.getDate()));
            Clock clock = new Clock();
            add(clock);
            setCellHorizontalAlignment(clock, VerticalPanel.ALIGN_CENTER);
            add(new HTML(Util.getLunarDayToday("<br>")));
          }
        });
      }
    });
  }

  private void draw_Calendar() {
    eastPanel.add(new Panel() {
      {
        setTitle("日历");
        setWidth(VALUE_PNLEAST_WIGHT);
        setFrame(true);
        setCollapsible(true);
        setIconCls("calendar-icon");

        DatePicker lunar = new DatePicker();
//        lunar.setMinDate(new Date(1901, 1, 1));
//        lunar.setMaxDate(new Date(2099, 12, 31));
        lunar.addListener(new DatePickerListenerAdapter() {
          public void onSelect(DatePicker dataPicker, Date date) {
            PopupPanel messagePanel = new PopupPanel(true);
            messagePanel
                .setPopupPosition(dataPicker.getAbsoluteLeft() + 10, dataPicker.getAbsoluteTop());
            messagePanel.setWidget(new HTML(getMessageHtml(Util.getLunarDay(date, "<br>"))));
            messagePanel.setWidth("150px");
            
            messagePanel.show();
          }
        });

        add(lunar);
      }
    });
  }

  private void draw_center() {
    centerPanel = new Panel();
    centerPanel.setBodyStyle(BORDERSTYLE_WHITE);
    mainPanel.add(centerPanel, new BorderLayoutData(RegionPosition.CENTER));

    centerPanel.add(new Panel() {
      {
        setFrame(false);
        setBodyBorder(false);
        setBorder(false);
        setWidth("100%");
        setHeight("100%");
      }
    });
  }

  private void loadAllCartridges() {
    CartridgeService.Locator.getService().getAllCartridges(new AsyncCallback() {
      public void onFailure(Throwable caught) {
        String message = "";
        if (caught.getMessage().indexOf("]") > 0
            && caught.getMessage().indexOf("]") != caught.getMessage().length() - 1)
          message = caught.getMessage().substring(caught.getMessage().indexOf("]") + 1);
        MessageBox.alert("错误", message);
      }

      public void onSuccess(Object result) {
        if (result != null && result instanceof List) {
          CartridgeViewPanel cvp = new CartridgeViewPanel();
          cvp.setCartridges((List) result);
          cvp.setId("cartridges");
          cvp.setWidth(VALUE_PNLCARTRIDGE_WIDTH);
          cvp.setAlignRight(true);
          cvp.setPictureSpec("logo_medium");

          pnlCartridge.add(cvp.showCartridges());
          pnlCartridge.doLayout();
        }
      }
    });
  }

  protected RBRemoteServiceAsync getRemoteService() {
    return WelcomeService.Locator.getService();
  }

  protected void onSessionLoad() {
    refreshWelcome();
  }

  private static native String getMessageHtml(String message) /*-{
   return ['<div class="msg">',
   '<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>',
   '<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc">', message, '</div></div></div>',
   '<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>',
   '</div>'].join('');
   }-*/;
}
